package com.me.launcher.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.text.TextUtils;

import com.me.launcher.bean.AppBean;
import com.me.launcher.bean.ShortCutEvent;
import com.me.launcher.db.DBUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

/**
 * @author hejiangwei
 * Created at 2018/5/14.
 */
public class ShortCutReceiver extends BroadcastReceiver {
    private static final String TAG = ShortCutReceiver.class.getName();
    public String Action = "com.android.launcher.action.INSTALL_SHORTCUT";

    @Override
    public void onReceive(Context context, Intent intent) {
        //接收安装广播com.lody.virtual.client.stub.ShortcutHandleActivity
        if (TextUtils.equals(intent.getAction(), Action)) {
            Intent intent1 = intent.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
            intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            String string = intent1.toUri(0);

            String name = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
            Bitmap icon = intent.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
            AppBean appBean = new AppBean.Builder()
                    .type(AppBean.AppType.LAUNCH_SHORT_CUT)
                    .name(name)
                    .intent(string)
                    .build();
            List<Integer> indexarr = DBUtil.getInstance(context).setAppsApp(
                    appBean);
            if (indexarr == null || indexarr.isEmpty()) {

            } else {

                EventBus.getDefault().post(new ShortCutEvent(0, indexarr, appBean));
            }

//            try {
//                context.startActivity(Intent.parseUri(string, 0));
//            } catch (URISyntaxException e) {
//                e.printStackTrace();
//            }
//        int user=intent1.getIntExtra("_VA_|_user_id_",0);
//            intent1.setAction("")
////            setShortCut(intent);
////            Intent launchIntent = intent.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
////            launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
////
////            intent.setSourceBounds(new Rect(pos[0], pos[1],
////                    pos[0] + v.getWidth(), pos[1] + v.getHeight()));
//
//
//            Intent startIntent = new Intent(intent);
////            startIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//            startIntent.setClass(context,intent.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT).getClass());
////            startIntent.setPackage()
////            startIntent.setClassName(intent.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT).activityInfo.packageName,
////                    ris.get(0).activityInfo.name);
//            startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//            context.startActivity(startIntent);


//            ComponentName componet = new ComponentName(info.packageName, info.name);
//
//            Intent i = new Intent();
//            i.setComponent(componet);
//            startActivity(i);

        }

    }

}
